package com.lxq.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.lxq.common.DbHelper;
import com.lxq.dao.UserCardDao;
import com.lxq.dao.impl.common.BaseDaoImpl;
import com.lxq.entity.UserCard;

public class UserCardDaoImpl extends BaseDaoImpl<UserCard> implements UserCardDao {

	@Override
	public String findAlias(String phoneNumber, String cardNumber) {
		String alias = new String();

		Connection conn = null;
		String sql = "select * from " + getTableName() + " where phoneNumber=? and cardNumber=?";
		try {
			conn = DbHelper.getConn();
			alias = qr.query(conn, sql, beanHandler, phoneNumber, cardNumber).getCardAlias();

		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			if (conn != null) {
				DbHelper.close(conn);
			}
		}

		return alias;
	}

	@Override
	public void updateAlias(String phoneNumber, String cardNumber, String cardAlias) {
		Connection conn = null;

		String sql = "update " + getTableName() + " set cardAlias=? where phoneNumber=? and cardNumber=?";
		try {

			conn = DbHelper.getConn();
			qr.update(conn, sql, cardAlias, phoneNumber, cardNumber);

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				DbHelper.close(conn);
			}
		}
	}

	@Override
	public List<String> findCards(String phoneNumber) {
		List<UserCard> usercards = null;

		Connection conn = null;
		
		String sql = "select cardNumber from " + getTableName() + " where phoneNumber=?";
		try {
			conn = DbHelper.getConn();
			usercards = qr.query(conn, sql, beanListHandler, phoneNumber);

		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			if (conn != null) {
				DbHelper.close(conn);
			}
		}
		
		List<String> cards = new ArrayList<String>();
		if(usercards == null){
			return cards;
		}
		
		for (UserCard userCard : usercards) {
			cards.add(userCard.getCardNumber());
		}

		return cards;
	}

	@Override
	public UserCard findByCardNumber(String cardNumber) {
		UserCard uc = null;

		Connection conn = null;
		String sql = "select * from " + getTableName() + " where cardNumber=?";
		try {
			conn = DbHelper.getConn();
			uc = qr.query(conn, sql, beanHandler, cardNumber);

		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			if (conn != null) {
				DbHelper.close(conn);
			}
		}

		return uc;
	}
	
	@Override
	public UserCard findByCardNumberPhone(String phoneNumber, String cardNumber) {
		UserCard uc = null;

		Connection conn = null;
		String sql = "select * from " + getTableName() + " where phoneNumber=? and cardNumber=?";
		try {
			conn = DbHelper.getConn();
			uc = qr.query(conn, sql, beanHandler, phoneNumber, cardNumber);

		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			if (conn != null) {
				DbHelper.close(conn);
			}
		}

		return uc;
	}

	@Override
	public void delete(String phoneNumber, String cardNumber) {

		Connection conn = null;
		String sql = "delete  from " + getTableName() + " where phoneNumber=? and cardNumber=?";
		try {
			conn = DbHelper.getConn();
			qr.update(conn, sql, phoneNumber, cardNumber);

		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			if (conn != null) {
				DbHelper.close(conn);
			}
		}
		
	}
}
